package cn.itzzx.lagou.datasource;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * TODO
 *
 * @author Martin
 * @since 1.0
 */
public class DataSourceHandler {
    private static DruidDataSource druidDataSource;

    public static DruidDataSource getDruidDataSource() {
        return druidDataSource;
    }

    public static void setConnentInfo(String url, String username, String password) {
        // 新建数据源
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        //释放之前连接
        if (druidDataSource != null) {
            druidDataSource.close();
        }
        // 设置新数据源
        druidDataSource = dataSource;

        //替换新的数据源
        JdbcTemplateFactory.jdbcTemplateSwitchSource();
    }
}
